function estimateTemplate() {
    var _this = this;
    var page;
    _this.index = -1;
    var items;
    _this.init = function (page_person) {
        page = page_person;
        _this.initTable();
    };

    _this.initTable = function () {
        var list_url = appPath + "/admin/estimate/page";
        page.init("template_form", list_url, "template_table", "template_page", 1, 10);
        page.goPage(1);
        page.list = function (dataList) {
            $("#checkAll").attr("checked", false);
            var len = dataList.length;
            items = dataList;
            var inner = "", item;
            // 组装数据
            for (var i = 0; i < len; i++) {
                item = dataList[i];
                inner += '<tr>';
                inner += '<td><input type="checkbox" name="ckbox"  value="' + item.id + '" /></td>';
                inner += '<td><span>' + item.estimateName + '</span></td>';
                inner += '<td><span>' + parseString(item.remark) + '</span></td>';
                inner += '<td><span>' + item.questionNum + '</span></td>';
                inner += '<td><span>' + item.createUser + '</span></td>';
                inner += '<td>' +parseFullDate(item.operDate) + '</td>';
                inner += '<td><a href="javascript:estimateTemplate.modify(\'' + item.id + '\');" id="btn_all" class="a a-view">修改</a> ';
                inner += '<a href="javascript:estimateTemplate.delete(\'' + item.id + '\');"  id="btn_edit" class="a a-view">删除</a></td>';
                inner += '</tr>';
            }
            return inner;
        }
    };

    _this.modify = function (varId) {
        window.location.href = appPath + "/admin/estimate/update?id=" + varId;
    }

    _this.delete = function (varId) {

        templateIdAll = varId
        _this.index = layer.alert('是否确定删除', {
            btn: ['确定', '取消'],
            btn1: function () {
                $.ajax({
                    url: appPath + "/admin/estimate/delete",
                    data: {idStr: templateIdAll.toString()},
                    dataType: "json",
                    async: true,
                    type: "POST",
                    success: function (data) {
                        var code = eval(data).code;
                        var result = eval(data).result;
                        if ("10000" == code) {
                            layer.msg("删除成功");
                            _this.initTable();
                        } else {
                            var tipMsg = statusTxt + "失败!";
                            var error = eval(data).errorMsg;
                            if(error != null && error != "") {
                                tipMsg += "\n" + error;
                            }

                        }
                    }
                });
            }
        })



    }

    // 新增按钮
    $("#btn_add").click(function () {
        window.location.href = appPath + "/admin/estimate/add";
    });

    _this.search = function () {
        _this.initTable();
    };
    _this.searchAll = function () {
        $("#all").val("");
        $("#all").val("");
        _this.initTable();
    };

    _this.ajax = function (url, param, type) {
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function (data) {
                result = data;

            }
        });
        return result;
    };

    var templateIdAll = [];
    var object = {};
    /**
     * 全选事件
     */
    $("#checkAll").click(function () {
            if (this.checked) {
                $("#template_table :checkbox:not(:disabled)").prop("checked", true);

                $("#template_table :checkbox:not(:disabled)").each(function () {
                    if (!object[$(this).val()]) {
                        object[$(this).val()] = 1;
                        templateIdAll.push($(this).val());
                    }
                });
            } else {
                $("#template_table :checkbox:not(:disabled)").prop("checked", false);
                if (templateIdAll.length > 0) {
                    for (var a = 0; a < templateIdAll.length; a++) {
                        $("#template_table :checkbox:not(:disabled)").each(function () {
                            if ($(this).val() === templateIdAll[a]) {
                                delete object[$(this).val()];
                                templateIdAll.splice(a, 1);
                            }
                        })
                    }
                }
            }
        }
    );

    /**
     * 单选事件
     */
    $("#template_table").on("click", "input[type=checkbox]", function () {
        if ($(this).is(":checked")) {
            if (!object[$(this).val()]) {
                object[$(this).val()] = 1;
                templateIdAll.push($(this).val());
            }
        } else {
            if (templateIdAll.length > 0) {
                for (var a = 0; a < templateIdAll.length; a++) {
                    if ($(this).val() === templateIdAll[a]) {
                        delete object[$(this).val()];
                        templateIdAll.splice(a, 1);
                    }
                }
            }
        }
        // console.log(templateIdAll);
    });
    /**
     *
     * 清空所有的选择
     */
    $("#clearSelAll").on("click", function () {
        object={};
        templateIdAll = [];
    });

    //删除按钮
    $("#btn_del").click(function () {
        if (templateIdAll.length===0) {
            layer.alert("请选择要删除的评估模板");
            return;
        }

        _this.index = layer.alert('是否确定删除', {
            btn: ['确定', '取消'],
            btn1: function () {
                $.ajax({
                    url: appPath + "/admin/estimate/delete",
                    data: {idStr: templateIdAll.toString()},
                    dataType: "json",
                    async: true,
                    type: "POST",
                    success: function (data) {
                        var code = eval(data).code;
                        var result = eval(data).result;
                        if ("10000" == code) {
                            layer.msg("删除成功");
                            _this.initTable();
                        } else {
                            var tipMsg = statusTxt + "失败!";
                            var error = eval(data).errorMsg;
                            if(error != null && error != "") {
                                tipMsg += "\n" + error;
                            }

                        }
                    }
                });
            }
        })


    });

}

function parseFullDate(str) {
    if(!str) return "";
    if(str.length<=10) return "";
    return str.substring(0,10);
}

function parseString(str) {
    if(!str) return "";

    return str;
}

var estimateTemplate = new estimateTemplate();